package cn.bytepath.log.controller;

import cn.bytepath.log.domain.UserLog;
import cn.bytepath.log.query.UserLogQuery;
import cn.bytepath.log.service.IUserLogService;
import cn.bytepath.util.PageInfo;
import cn.bytepath.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户日志的controller层
 * @author Jack.C
 * @createDate 2022-02-07
 * @version 1.0
 */
@Api(tags = "用户日志接口",description = "用户日志接口详细描述")
@RestController
@RequestMapping("/userlog")
public class UserLogController {

    /**
     * 注入用户日志业务层对象
     */
    @Autowired
    private IUserLogService userLogService;

    @ApiOperation("高级查询并分页")
    @PostMapping
    public PageInfo<UserLog> pageQuery(@RequestBody UserLogQuery query) {
        return userLogService.findPageInfoByQuery(query);
    }

    @ApiOperation("根据id删除单个用户日志记录")
    @DeleteMapping("/{id}")
    public PageResult delete(@PathVariable("id") Long id) {
        try {
            userLogService.delete(id);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation(value = "用户日志添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public PageResult saveOrUpdate(@RequestBody UserLog userLog) {
        try {
            System.out.println(userLog);
            userLogService.saveOrUpdate(userLog);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }

    @ApiOperation("根据id批量删除用户日志记录")
    @PatchMapping
    public PageResult deleteBatch(@RequestBody Long[] ids) {
        try {
            userLogService.deleteBatch(ids);
            return PageResult.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return PageResult.FAIL.setMsg(e.getMessage());
        }
    }
}
